﻿using CMS.BaseEntity;
using ElectronicSelling.BusinessLogic;
using ElectronicSelling.Common;
using ElectronicSelling.Utilities;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace ElectronicSelling.UserControl
{
    public partial class OrderProcess : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["cart"] == null)
            {
                Response.Redirect(Directory.Product_List_Page);
            }
            else
            {
                GetCartToCheckOut();
                GetCustomerDetail();
            }
        }

        protected void GetCartToCheckOut()
        {
            if (Session["cart"] != null)
            {
                DataTable cart = Session["cart"] as DataTable;
                DataTable dt = CartLogic.GetCartFromSession(cart);
                rptCartDetail.DataSource = dt;
                rptCartDetail.DataBind();
            }
        }

        public double GetTotalOrder()
        {
            try
            {
                DataTable cart = Session["cart"] as DataTable;
                return CartLogic.GetTotalDouble(cart);
            }
            catch (Exception)
            {
                //Response.Redirect(Directory.Error);
                return 0;
            }
        }

        protected void GetCustomerDetail()
        {
            if (Session["UserId"] != null)
            {
                User user = new User();
                user.UserId = int.Parse(Session["UserId"].ToString());
                User userObject = HomeAccountLogic.GetCusTableToObject(user);
                txtUserName.Text = userObject.UserName;
                txtPhone.Text = userObject.Phone;
                txtFullName.Text = userObject.FullName;
                txtEmail.Text = userObject.Email;
                txtDOB.Text = userObject.DateOfBirth;
                txtAddress.Text = userObject.Address;
            }

        }

        protected void chkEdit_CheckedChanged(object sender, EventArgs e)
        {
            if (chkEdit.Checked == false)
            {
                txtUserName.Enabled = false;
                txtPhone.Enabled = false;
                txtFullName.Enabled = false;
                txtDOB.Enabled = false;
                txtAddress.Enabled = false;
                txtEmail.Enabled = false;
                radioGender.Enabled = false;
            }
            else
            {
                txtUserName.Enabled = false;
                txtUserName.Focus();
                txtPhone.Enabled = true;
                txtFullName.Enabled = true;
                txtDOB.Enabled = true;
                txtAddress.Enabled = true;
                txtEmail.Enabled = true;
                radioGender.Enabled = true;
            }
        }

        protected void btnUpdateCustomer_Click(object sender, EventArgs e)
        {
            User customer = new User();
            customer.UserId = int.Parse(Session["UserId"].ToString());
            customer.UserName = txtUserName.Text;
            customer.Phone = txtPhone.Text;
            customer.FullName = txtFullName.Text;
            customer.Email = txtEmail.Text;
            customer.DateOfBirth = txtDOB.Text;
            customer.Address = txtAddress.Text;
            if (radioGender.SelectedValue == "1")
                customer.Gender = "True";
            else
                customer.Gender = "False";
            if (HomeAccountLogic.IsUpdateCustomerForOrder(customer))
            {
                GetCustomerDetail();
                Response.Write(HTMLString.Manipulation_Success);
            }
            else
            {
                Response.Write(HTMLString.Manipulation_Fail);
            }
        }

        protected void btnSendOrder_Click(object sender, EventArgs e)
        {
            DataTable cart = Session["cart"] as DataTable;
            if (cart.Rows.Count > 0)
            {
                try
                {
                    Order order = new Order();
                    order.CustomerId = int.Parse(Session["UserId"].ToString());
                    order.CreateDate = DateTime.Now;
                    order.Total = CartLogic.GetTotalDouble(cart);
                    order.Note = txtNote.Text;
                    bool isError = false;
                    if (OrderLogic.IsInsertOrder(order))
                    {
                        int maxOrderIdInserted = OrderLogic.GetMaxOrderId();
                        foreach (DataRow dr in cart.Rows)
                        {
                            OrderDetail orderDetail = new OrderDetail();
                            orderDetail.OrderId = maxOrderIdInserted;
                            orderDetail.ProductId = int.Parse(dr["ProductId"].ToString());
                            orderDetail.Quantity = int.Parse(dr["Quantity"].ToString());
                            if (OrderLogic.IsInsertOrderDetail(orderDetail))
                            {
                            }
                            else
                            {
                                isError = true;
                                Response.Write(HTMLString.Manipulation_Fail);
                            }
                        }

                    }
                    else
                    {
                        isError = true;
                        Response.Write(HTMLString.Manipulation_Fail);
                    }
                    if (!isError)
                    {
                        Session["OrderSuccess"] = true;
                        //Response.Write(HTMLString.Order_Success);
                        btnSendOrder.Text = "Bạn đã gửi đơn hàng này!";
                        btnSendOrder.Enabled = false;
                    }
                }
                catch (Exception)
                {
                    //Response.Redirect(Directory.Error);
                }
            }
            else
            {
                Response.Write(HTMLString.Cart_Null);
            }
        }
    }
}